#include "AudioDeviceObserverImpl.h"


AudioDeviceObserverImpl::AudioDeviceObserverImpl(void)
{
	m_mid_OnErrorIsReported = 0;
	m_mid_OnWarningIsReported = 0;

	m_cls_ErrorCode = 0;
	m_mid_ErrorCode_fromValue = 0;
	m_cls_WarningCode = 0;
	m_mid_WarningCode_fromValue = 0;
}

AudioDeviceObserverImpl::~AudioDeviceObserverImpl(void)
{
}

bool AudioDeviceObserverImpl::init_jni(JNIEnv* env)
{
	bool result = false;

	do 
	{
		m_mid_OnErrorIsReported = (env)->GetMethodID(m_cls, "onErrorIsReported", "(Lcom/voe/callback/AudioDeviceObserver$ErrorCode;)V");
		if(!m_mid_OnErrorIsReported)
			break;

		m_mid_OnWarningIsReported = (env)->GetMethodID(m_cls, "onWarningIsReported", "(Lcom/voe/callback/AudioDeviceObserver$WarningCode;)V");
		if(!m_mid_OnWarningIsReported)
			break;

		m_cls_ErrorCode = (env)->FindClass("Lcom/voe/callback/AudioDeviceObserver$ErrorCode;");
		if(!m_cls_ErrorCode)
			break;

		m_mid_ErrorCode_fromValue = (env)->GetStaticMethodID(m_cls_ErrorCode, "fromValue", "(I)Lcom/voe/callback/AudioDeviceObserver$ErrorCode;");
		if(!m_mid_ErrorCode_fromValue)
			break;

		m_cls_WarningCode = (env)->FindClass("Lcom/voe/callback/AudioDeviceObserver$WarningCode;");
		if(!m_cls_WarningCode)
			break;

		m_mid_WarningCode_fromValue = (env)->GetStaticMethodID(m_cls_WarningCode, "fromValue", "(I)Lcom/voe/callback/AudioDeviceObserver$WarningCode;");
		if(!m_mid_WarningCode_fromValue)
			break;

		result = true;

	} while (false);

	return result;
}

void AudioDeviceObserverImpl::OnErrorIsReported(const ErrorCode error)
{
	JNIEnv* env = 0;
	if(before(&env))
	{
		jobject ret = (env)->CallStaticObjectMethod(m_cls_ErrorCode, m_mid_ErrorCode_fromValue, error);
		if(ret)
		{
			(env)->CallObjectMethod(m_obj, m_mid_OnErrorIsReported, ret);
		}
		after(env);
	}
	
}

void AudioDeviceObserverImpl::OnWarningIsReported(const WarningCode warning)
{
	JNIEnv* env = 0;
	if(before(&env))
	{
		jobject ret = (env)->CallStaticObjectMethod(m_cls_WarningCode, m_mid_WarningCode_fromValue, warning);
		if(ret)
		{
			(env)->CallObjectMethod(m_obj, m_mid_OnWarningIsReported, ret);
		}
		after(env);
	}
}
